package yongjian.drp.dao.Impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import yongjian.drp.dao.ItemDao;
import yongjian.drp.model.Client;
import yongjian.drp.model.Item;

@Component("itemDao")
public class ItemDaoImpl implements ItemDao{
	
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#isExist(java.lang.String)
	 */
	@Override
	public boolean isExist(String code) {
		List<Item> items = new ArrayList<Item>();
		items = sessionFactory.getCurrentSession().createQuery("from Item i where i.code = '" + code + "'").list();
		if(items.size() > 0){
			return true;
		}
		return false;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#add(yongjian.drp.model.Item)
	 */
	@Override
	public void add(Item item) {
		sessionFactory.getCurrentSession().save(item);
	}

	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#modify(yongjian.drp.model.Item)
	 */
	@Override
	public void modify(Item item) {
		if(item == null)return;
		Item i = (Item)sessionFactory.getCurrentSession().load(Item.class, item.getId());
		i.setName(item.getName());
		i.setCategory(item.getCategory());
		i.setCode(item.getCode());
		i.setPattern(item.getPattern());
		i.setSpec(item.getSpec());
		i.setUnit(item.getUnit());
		i.setPrice(item.getPrice());
		sessionFactory.getCurrentSession().update(i);
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#delete(int)
	 */
	@Override
	public void delete(int id) {
		sessionFactory.getCurrentSession().createQuery("delete from Item u where u.id=?").setParameter(0, id).executeUpdate();
	}
	/**
	 * ƴװHQL
	 * @param key
	 * @return
	 */
	public String createHql(String key){
		String hql = "from Item u where 1=1";
		if(key !=null && !key.equals("")){
			hql += " and u.code like '%" + key +"%' or u.name like'%" + key + "%'";
		}
		return hql;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#queryByPage(int, int, java.lang.String)
	 */
	@Override
	public List<Item> queryByPage(int page, int rows, String key) {
		String hql = createHql(key);
		List<Item> items = (List<Item>)sessionFactory.getCurrentSession().createQuery(hql)
							.setFirstResult((page-1)*rows).setMaxResults(rows).list();
		
		return items;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#getAllRecord(java.lang.String)
	 */
	@Override
	public int getAllRecord(String key) {
		String hql = createHql(key);
		int count = sessionFactory.getCurrentSession().createQuery(hql).list().size();
		return count;
	}
	/*
	 * (non-Javadoc)
	 * @see yongjian.drp.dao.ItemDao#getAllItem()
	 */
	@Override
	public List<Item> getAllItem() {
		
		return (List<Item>)sessionFactory.getCurrentSession().createQuery("from Item").list();
	}
	
	

	
	

}
